package com.car.loan.project.xr.controller;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.framework.aspectj.lang.annotation.DataStatus;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.service.ILoanService;
import com.car.loan.project.xr.service.ILoanStatusService;
import com.car.loan.project.xr.service.loan.payment.LoanPaymentService;
import com.car.loan.project.xr.util.bo.business.LoanBaseInfo;
import com.car.loan.project.xr.util.bo.business.payment.LoanPaymentApplyBo;
import com.car.loan.project.xr.util.enums.PaymentApplyStatus;
import com.car.loan.project.xr.util.enums.PaymentAuditStatus;
import com.car.loan.project.xr.util.enums.PaymentInfoStatus;
import com.car.loan.project.xr.util.select.LoanBasePageParam;
import com.car.loan.project.xr.util.vo.PaymentApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @description
 * @date 2019/11/26 17:05
 */
@RestController
@RequestMapping("xr/payment/audit")
@Api(value = "付款审核", tags = "车贷业务-付款审核")
public class LoanPaymentAuditController {


    @Autowired
    private LoanPaymentService loanPaymentService;

    @Autowired
    private ILoanService loanService;

    @Autowired
    private ILoanStatusService loanStatusService;


    @PreAuthorize("@ss.hasPermi('payment:audit:list')")
    @GetMapping("list")
    @ApiOperation(value = "获取付款审核列表", response = LoanBaseInfo.class, responseContainer = "list")
    @DataStatus(loanStatusEnum = com.car.loan.common.enums.LoanStatusEnum.PAYMENT_AUDIT_STATUS)
    public AjaxResult applyList(@Valid LoanBasePageParam loanBasePageParam) {
        return AjaxResult.success(loanService.page(loanBasePageParam));
    }


    @PreAuthorize("@ss.hasPermi('payment:audit:query')")
    @GetMapping("{loanId}")
    @ApiOperation(value = "获取付款审核详情", response = PaymentApplyVo.class)
    public AjaxResult saveApply(@PathVariable Long loanId) {
        return loanPaymentService.info(loanId);
    }


    @PreAuthorize("@ss.hasPermi('payment:audit:add')")
    @PostMapping("/{loanId}/{operatorStatus}")
    @Log(title = "审核付款提交", businessType = BusinessType.UPDATE)
    @ApiOperation("审核付款提交")
    public AjaxResult saveApply(@PathVariable Long loanId,
        @ApiParam("0=保存 1=提交") @PathVariable Long operatorStatus) {
        return loanPaymentService.audit(loanId, operatorStatus);
    }


    @PreAuthorize("@ss.hasPermi('payment:apply:withdraw')")
    @PutMapping("withdraw/{loanId}")
    @Log(title = "撤回付款审核", businessType = BusinessType.WITHDRAW)
    @ApiOperation(value = "撤回付款审核", notes = "付款信息填写后不可撤回")
    public AjaxResult withdrawApply(@PathVariable Long loanId) {
        Optional<LoanStatus> optionalLoanStatus = loanStatusService.getByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("业务状态数据异常请联系管理员"));
        if (PaymentInfoStatus.SUBMIT.status == loanStatus.getPaymentInfoStatus()) {
            return AjaxResult.error("付款信息已填写，不可撤回");
        }
        loanStatus.setPaymentAuditStatus((long) PaymentAuditStatus.INIT.status);
        loanStatusService.updateLoanStatus(loanStatus);
        return AjaxResult.success();
    }

}
